package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dtos.PageResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.exception.LeadException;
import com.heima.model.admin.dtos.ChannelDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.lang.invoke.LambdaConversionException;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2022-09-13
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 分页查询频道
     * @param dto
     * @return
     */
    @Override
    public PageResult<AdChannel> findByPage(ChannelDto dto) {

//        设置分页
        IPage<AdChannel> ipage = new Page<>(dto.getPage(),dto.getSize());
//        设置查询
        LambdaQueryWrapper<AdChannel> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.like(StringUtils.isNotBlank(dto.getName()),AdChannel::getName,dto.getName());
        IPage<AdChannel> channelIPage = page(ipage, queryWrapper);
        if(CollectionUtils.isEmpty(channelIPage.getRecords())){
            throw new LeadException(AppHttpCodeEnum.CHANNEL_NOT_FOUND);
        }

        return new PageResult<>(dto.getPage(),
                dto.getSize(),
                channelIPage.getTotal(),
                channelIPage.getRecords());
    }
}
